package panel.control;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import panel.event.LightSelectedEvent;
import panel.event.LightSelectedListener;
import shape.Light;
import window.Command;


public class LightPanel extends JPanel {
	LightSelectedListener lightControl;
	ActionListener actionControl;

	LightField lightList[];
	int lightCount;

	JButton add, delete;
	JLabel title;
	GridBagConstraints c;

	public LightPanel( ActionListener actionControl, LightSelectedListener lightControl ) {
		setLayout(new GridBagLayout());
		setOpaque(false);

		this.lightControl = lightControl;
		this.actionControl = actionControl;

		c 			= new GridBagConstraints();
		c.insets 	= new Insets(0, 0, 5, 0);
		c.fill 		= GridBagConstraints.BOTH;
		c.gridx 	= 0;
		c.gridy 	= GridBagConstraints.RELATIVE;
		c.gridwidth = 2;
		c.weightx	= 1.0;
		c.weighty 	= 1.0;

		title = new JLabel("Lights");
		add(title, c);

		lightList = new LightField[Light.MAX_LIGHTS];
		lightCount = 0;

		/*
		 * All checkboxes will be created.
		 * They will be added/removed from the panel as
		 * needed, updating the associated light object.
		 */
		
		for (int i = 0; i < Light.MAX_LIGHTS; i++) {
			lightList[i] = new LightField();
		}

		c.gridwidth = 1;
		c.insets.right = 2;

		add = new JButton("Add");
		add.setActionCommand(String.valueOf(Command.ADD_LIGHT));
		add.addActionListener(actionControl);
		add(add, c);

		c.gridx = 1;
		c.insets.right = 0;
		c.insets.left = 2;

		delete = new JButton("Del");
		delete.setActionCommand(String.valueOf(Command.DELETE_LIGHT));
		delete.addActionListener(actionControl);
		add(delete, c);

		c.gridx = 0;
		c.gridwidth = 2;
		c.insets.right = 0;
		c.insets.left = 0;
		c.insets.bottom = 0;
	}

	public void updateLights( Light list[] ) {
		for (int i = 0; i < list.length; i++) {
			
			// If new list is longer than currently displayed
			// list, add checkboxes.
			if (i >= lightCount) {
				add(lightList[i].box, c );
			}
			
			lightList[i].update(list[i]);
		}
		
		// If new list is shorter than currently displayed
		// list, remove checkboxes.
		for (int i = list.length; i < lightCount; i++)
			remove(lightList[i].box);
		
		updateUI();
		lightCount = list.length;
	}

	public Light[] getSelectedLights() {
		ArrayList<Light> list = new ArrayList<Light>();
		Light[] array;

		for (LightField field: lightList)
			if (field.box.isSelected()) {
				list.add(field.light);
				field.box.setSelected(false);
			}

		array = new Light[list.size()];
		list.toArray(array);
		return array;
	}

	private class LightField implements ActionListener {
		Light light;
		JCheckBox box;

		public LightField() {
			this.box = new JCheckBox();
			box.addActionListener(this);
			box.setOpaque(false);
		}

		public LightField( Light light ) {
			this();
			update(light);
		}

		public void update( Light light ) {
			this.light = light;
			this.box.setText("Light " + light.getIndex());
		}

		public void actionPerformed(ActionEvent e) {
			LightSelectedEvent le = new LightSelectedEvent(light, box.isSelected());
			lightControl.lightSelected(le);
		}
	}
}
